package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportUploadFilesRpcService;
import com.hunttown.mes.rpc.domain.TransportUploadFilesDTO;
import com.hunttown.mes.rpc.domain.query.TransportUploadFilesDTOQuery;
import com.hunttown.mes.service.TransportUploadFilesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportUploadFilesConverts.convert;

/**
 * created by wangjunfu on 2020-06-14 12:47:48
 */
@Service
public class TransportUploadFilesRpcServiceImpl implements TransportUploadFilesRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportUploadFilesRpcServiceImpl.class);

    private final TransportUploadFilesService transportUploadFilesService;

    @Autowired
    public TransportUploadFilesRpcServiceImpl(TransportUploadFilesService transportUploadFilesService) {
        this.transportUploadFilesService = transportUploadFilesService;
    }

    @Override
    public TransportUploadFilesDTO insert(TransportUploadFilesDTO transportUploadFilesDTO) {
        try {
            return convert(transportUploadFilesService.insert(convert(transportUploadFilesDTO)));
        } catch (Exception e) {
            logger.error("TransportUploadFiles create failed.", e);
            throw new RuntimeException("TransportUploadFiles create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportUploadFilesDTO transportUploadFilesDTO) {
        try {
            return transportUploadFilesService.updateInfoById(convert(transportUploadFilesDTO));
        } catch (Exception e) {
            logger.error("TransportUploadFiles update by id failed.", e);
            throw new RuntimeException("TransportUploadFiles update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportUploadFilesDTOQuery transportUploadFilesDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportUploadFilesDTOQuery.getW_id() != null && transportUploadFilesDTOQuery.getW_id() > 0) {
                query.put("w_id", transportUploadFilesDTOQuery.getW_id());
            }

            if (transportUploadFilesDTOQuery.getW_tableId() != null && transportUploadFilesDTOQuery.getW_tableId() > 0) {
                query.put("w_tableId", transportUploadFilesDTOQuery.getW_tableId());
            }

            if (transportUploadFilesDTOQuery.getW_recordId() != null && transportUploadFilesDTOQuery.getW_recordId() > 0) {
                query.put("w_recordId", transportUploadFilesDTOQuery.getW_recordId());
            }

            if (transportUploadFilesDTOQuery.getIds() != null && !transportUploadFilesDTOQuery.getIds().equals("")) {
                query.put("ids", transportUploadFilesDTOQuery.getIds());
            }

            if (transportUploadFilesDTOQuery.getRecordId() != null) {
                query.put("recordId", transportUploadFilesDTOQuery.getRecordId());
            }

            if (transportUploadFilesDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUploadFilesDTOQuery.getStopFlag());
            }

            if (transportUploadFilesDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportUploadFilesDTOQuery.getDeleteFlag());
            }

            return transportUploadFilesService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportUploadFiles update by query failed.", e);
            throw new RuntimeException("TransportUploadFiles update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUploadFilesDTO selectById(Integer id) {
        try {
            return convert(transportUploadFilesService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportUploadFiles select by id failed.", e);
            throw new RuntimeException("TransportUploadFiles select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUploadFilesDTO selectObjByQuery(TransportUploadFilesDTOQuery transportUploadFilesDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(transportUploadFilesDTOQuery.getEtag())) {
                query.put("etag", transportUploadFilesDTOQuery.getEtag());
            }

            if (transportUploadFilesDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUploadFilesDTOQuery.getStopFlag());
            }

            return convert(transportUploadFilesService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportUploadFiles select obj by query failed.", e);
            throw new RuntimeException("TransportUploadFiles select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUploadFilesDTO selectObjByQueryWithoutEntId(TransportUploadFilesDTOQuery transportUploadFilesDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(transportUploadFilesDTOQuery.getEtag())) {
                query.put("etag", transportUploadFilesDTOQuery.getEtag());
            }

            if (transportUploadFilesDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUploadFilesDTOQuery.getStopFlag());
            }

            return convert(transportUploadFilesService.selectObjByQueryWithoutEntId(query));
        } catch (Exception e) {
            logger.error("TransportUploadFiles select obj by query failed.", e);
            throw new RuntimeException("TransportUploadFiles select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportUploadFilesDTO> getListForPage(TransportUploadFilesDTOQuery transportUploadFilesDTOQuery) {
        Query query = Query.page(transportUploadFilesDTOQuery.getPn(), transportUploadFilesDTOQuery.getPageSize());

        if (transportUploadFilesDTOQuery.getId() != null && transportUploadFilesDTOQuery.getId() > 0) {
            query.put("id", transportUploadFilesDTOQuery.getId());
        }

        if (transportUploadFilesDTOQuery.getIds() != null && !transportUploadFilesDTOQuery.getIds().equals("")) {
            query.put("ids", transportUploadFilesDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportUploadFilesDTOQuery.getLikeName())) {
            query.put("likeName", transportUploadFilesDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportUploadFilesDTOQuery.getShowDate())) {
            query.put("showDate", transportUploadFilesDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportUploadFilesDTOQuery.getStartDate())) {
            query.put("startDate", transportUploadFilesDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportUploadFilesDTOQuery.getEndDate())) {
            query.put("endDate", transportUploadFilesDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportUploadFilesDTOQuery.getOrderBy())) {
            query.put("orderBy", transportUploadFilesDTOQuery.getOrderBy());
        }

        List<TransportUploadFilesDTO> list = convert(transportUploadFilesService.selectByQuery(query));
        Page<TransportUploadFilesDTO> page = Page.from(transportUploadFilesDTOQuery.getPn(), transportUploadFilesDTOQuery.getPageSize(), list);
        page.setTotalItems(transportUploadFilesService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportUploadFilesService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportUploadFiles delete failed.", e);
            throw new RuntimeException("TransportUploadFiles delete failed, Please order your code.", e);
        }
    }
}